package com.cswnode.demo.service.impl;

import com.cswnode.demo.dao.PostDao;
import com.cswnode.demo.entity.Post;
import com.cswnode.demo.enums.YesOrNoEnum;
import com.cswnode.demo.service.PostService;
import com.cswnode.demo.util.Utils;
import com.cswnode.demo.vo.PostVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 岗位(Post)表服务实现类
 *
 * @author makejava
 * @since 2021-01-26 11:10:10
 */
@Service
public class PostServiceImpl implements PostService {
    @Autowired
    private PostDao postDao;

    /**
     * 通过ID查询单条数据
     *
     * @param postId 主键
     * @return 实例对象
     */
    @Override
    public Post queryById(Long postId) {
        return this.postDao.queryById(postId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Post> queryAllByLimit(int offset, int limit) {
        return this.postDao.queryAllByLimit(offset, limit);
    }

    @Override
    public List<PostVo> queryAllVo(int offset, int limit, String postName, List<Long> entId) {
        return postDao.queryVoAll(offset, limit, postName, entId);
    }

    @Override
    public int selectNum(String postName, List<Long> entId) {
        return postDao.selectNum(postName,entId);
    }

    /**
     * 新增数据
     *
     * @param post 实例对象
     * @return 实例对象
     */
    @Override
    public Post insert(Post post) {
        if (null == post.getProId()){
            return null;
        }
        if (null == post.getEntId()){
            return null;
        }
        post.setIsDelete(YesOrNoEnum.NO.getCode());
        post.setUserId(Utils.getLoginUserId());
        post.setCreateTime(new Date());
        post.setPostId(null);
        if (null == post.getUserId()){
            return null;
        }
        postDao.insert(post);
        return post;
    }

    /**
     * 修改数据
     *
     * @param post 实例对象
     * @return 实例对象
     */
    @Override
    public Post update(Post post) {
        post.setCreateTime(null);
        this.postDao.update(post);
        return this.queryById(post.getPostId());
    }

    /**
     * 通过主键删除数据
     *
     * @param postId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long postId) {
        return this.postDao.deleteById(postId) > 0;
    }
}